<template>
    <div class="home">
        <div class="search">
            <router-link to="/home/city" tag="div" class="location">{{this.area}}</router-link>
            <van-cell-group >
                <van-field
                    v-model="value"
                    left-icon="search"
                    placeholder="搜索商品"
                    class="ipt"
                    @focus="toSearch"
                />
            </van-cell-group>
            <div id="allmap"></div>
        </div>
        <div class="swiper">
            <van-swipe class="my-swipe" :autoplay="3000" >
                <van-swipe-item v-for="item in banner" :key="item.id" >
                    <img :src="item.image_url">
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="channel">
            <van-grid :column-num="5">
                <van-grid-item v-for="item in channel" :key="item.id"  :icon="item.icon_url" :text="item.name" :to="`/category/categorylist?id=${item.id}`" />
            </van-grid>
        </div>
        <div class="brand">
            <router-link class="head" tag="div" to="/brand"> 品牌制造商直供 </router-link>
            <div class="brand_content" >
                <router-link v-for="item in brandList" :key="item.id" class="brand_list" tag="div" :to="`/brand/detailaction?id=${item.id}`"> 
                    <img :src="item.new_pic_url"  class="brand_img"/>
                    <div class="info">
                        <p>{{item.name}}</p>
                        <p>{{item.floor_price}}元起</p>
                    </div>
                </router-link>
            </div>
        </div>
        <div class="newgoods">
            <router-link class="newgoods_top"  tag="div" to="/new/newgoods?isNew=1">
                <div class="top">
                    <p>新品首发</p>
                    <p>查看全部</p>
                </div>
            </router-link>  
            <div class="list">
                <router-link  v-for="item in newGoods" :key="item.id" class="single" :to="`/cart/goods?id=${item.id}`" tag="div">
                    <img :src="item.list_pic_url" alt="">
                    <p>{{item.name}}</p>
                    <p>{{item.goods_brief}}</p>
                    <p>￥{{item.retail_price}}</p>
                </router-link>
            </div>
        </div>
        <div class="hotgoods">
            <router-link class="hotgoods_top" to="/new/newgoods?isHot=1">
                <div class="top">
                    <p>人气推荐·好物精选</p>
                    <p>查看全部</p>
                </div>
            </router-link>  
            <div class="list">
                <router-link v-for="item in hotGoods" :key="item.id" class="single" :to="`/cart/goods?id=${item.id}`" tag="div">
                    <img :src="item.list_pic_url" alt="">
                    <p>{{item.name}}</p>
                    <p>{{item.goods_brief}}</p>
                    <p>￥{{item.retail_price}}</p>
                </router-link>
            </div>
        </div>
        <div class="topiclist">
            <div class="topiclist_top">
                专题精选<span class="icon"></span>
            </div>  
            <div class="list">
                <router-link v-for="item in topicList" :key="item.id" class="single" tag="div" :to="`/topic/detail?id=${item.id}`" >
                    <img :src="item.scene_pic_url" alt="">
                    <div class="btm">
                        <div>
                            <p>{{item.title}}</p>
                            <p>{{item.subtitle}}</p>
                        </div>
                        <div>{{item.price_info}}元起</div>
                    </div>
                </router-link>
            </div>
        </div>
        <div class="newcategory">
            <div class="list" v-for="items in newCategoryList" :key="items.id">
                <div class="head">{{items.name}}好物</div>
                <div class="sublist">
                   <router-link class="single"  v-for="item in items.goodsList" :key="item.id" :to="`/cart/goods?id=${item.id}`" tag="div">
                        <img :src="item.list_pic_url" alt="">
                        <p>{{item.name}}</p>
                        <p>￥{{item.retail_price}}</p>
                   </router-link>
                    <router-link class="single last"  :to="`/category/categorylist?id=${items.id}`" tag="div">
                       <div class="last-box">
                            <p>{{items.name}}好物</p>
                            <span class="icon"></span>
                       </div>
                    </router-link>
                </div>
            </div>
        </div>

    </div>
</template>

<script>
    import { home} from '@/api/home'
    import { mapMutations, mapState } from "vuex"
    export default {
        data() {
            return {
                value:'',
                banner:[],
                channel:[],
                brandList:[],
                newGoods:[],
                hotGoods:[],
                topicList:[],
                newCategoryList:[],
                area:''

            }
        },
        created(){
            home()
            .then((res) => {
                // console.log(res);
                this.banner=res.banner
                this.channel=res.channel
                this.brandList=res.brandList
                this.newGoods=res.newGoods
                this.hotGoods=res.hotGoods
                this.topicList=res.topicList
                this.newCategoryList=res.newCategoryList
            })
        },
        methods: {
            toCategoryMain(id){
                this.$router.push({
                    path:'/category/main',
                    query:{id:id}
                })
            },
            toSearch(){
                this.$router.push({
                    path:"/home/search"
                })
            },
            ...mapMutations(["setInfo"]),
        },
        mounted() {
            var _this=this;
            var map = new BMap.Map("allmap");    // 创建Map实例
            this.area=this.$store.state.cityInfo.title
           if(this.$store.state.cityInfo==""){
                map.centerAndZoom(new BMap.Point(113.754407,34.776794), 11);  // 初始化地图,设置中心点坐标和地图级别
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r){
                    if(this.getStatus() == BMAP_STATUS_SUCCESS){
                    // console.log(r);
                    var mk = new BMap.Marker(r.point);
                    map.addOverlay(mk);
                    map.panTo(r.point);
                    _this.setInfo(r); 
                    _this.area=r.address.city 
                    console.log( _this. $store.state.cityInfo ); 
                    }
                    else {
                        alert('failed'+this.getStatus());
                    }        
                },{enableHighAccuracy: true})
           }

            
        },
    }
</script>

<style lang="scss" scoped>


.home{
    background-color: #eee;
}
    .search {
        display: flex;
        align-items: center;
        background-color: #fff;

        .location{
            width:70px;
        }

    }
    .swiper{
        .my-swipe .van-swipe-item{
            height: 200px;
        }
        img{
            width: 100%;
            height: 100%;
        }
    }
    .brand {
        margin-top: 10px;
        background-color: #fff;

        .head{
            display: block;
            text-align: center;
            padding: 20px 0;
            font-size: 12px;
        }

        .brand_content{
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;


            .brand_list{
                position: relative;
                width: 46%;
                padding: 5px;

                .brand_img{
                    width: 100%;
                    height: 100%;
                }
                .info{
                    position: absolute;
                    top:0;
                    left: 12px;
                    text-align: left;

                    p{
                        font-size: 12px;
                    }
                }
            }
        }
    }
    @mixin text_over{
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    @mixin list_text{
        
        display: flex;
        background: #fff;
        padding-top: 10px;
        padding-bottom: 5px;
        white-space: nowrap;
        overflow: auto;

        .single{
            width: 140px;
            margin-left: 12px;
            
            img{
                width: 140px;
                height: 140px;
            }
            p{
                width: 132px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                text-align: left;
                margin: 0 4px;
                margin-bottom: 16px;
                padding: 0 5px;  
            }

            p:nth-child(2){
                font-size: 15px;
                font-weight: 700;
            }
            p:nth-child(3){
                color: #8a8a8a;
                font-size: 12px;
            }
            p:nth-child(4){
                color: #9c3232;
                font-size: 12px;
            }

            
            
        }
   
}

.newgoods{

    .newgoods_top{
        margin-top: 10px;
        background: url('./../../assets/images/bgnew.png') no-repeat;
        height: 130px;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;

        .top{

            p:nth-child(1){
                font-size: 20px;
                color: #8c9bae;
            }

            p:nth-child(2){
                font-size: 12px;
                color: #8c9bae;
                background: #d8e4f0;
                height: 25px;
                line-height: 25px;
                margin-bottom: 0;
            }
        
        }
    }   
        .list{
                @include list_text; 

        }
}

.hotgoods{
    
     .hotgoods_top{
        margin: 10px 0;
        background: url('./../../assets/images/bgtopic.png') no-repeat;
        height: 130px;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;

        .top{

            p:nth-child(1){
                font-size: 20px;
                color: #b1a279;
            }

            p:nth-child(2){
                font-size: 12px;
                color: #b1a279;
                background:#f4e9cb;
                height: 25px;
                line-height: 25px;
                margin-bottom: 0;
            }
        
        }
    } 
    .list{
        @include list_text;
    }  
}

.topiclist{
    margin-top: 10px;
    background: #fff;

    .topiclist_top{
        text-align: center;
        padding: 18px;
        vertical-align: middle;
        font-size: 12px;
        
        .icon{
            display: inline-block;
            width: 16px;
            height: 16px;
            margin-left: 2.5px;
            background: url('./../../assets/images/right.png') no-repeat;
            background-size: 100% 100%;
            vertical-align: middle;
        }
    }
    .list{
        display: flex;
        flex-wrap: nowrap;
        overflow: auto;

        .single{
            margin-left: 12px;
             width: 287px;

            img{
                width: 287px;
                height: 162px;
            }

            .btm{
                width: 100%;
                display: flex;
                white-space: nowrap;
                justify-content: space-between;

                div:nth-child(1){
                    width: 80%;

                    p:nth-child(1){
                        font-weight: 700;
                        font-size: 15px;
                        text-align: left;
                        margin-top: 4px;
                        margin-bottom: 15px;
                    }

                    p:nth-child(2){
                        color: #8a8a8a;
                        width: 90%;
                        font-size: 12px;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                    }
                }
               
                div:nth-child(2){
                    margin-top: 4px;
                    color: #9c3232;
                    font-size: 12px;
                }
            }

        }

    }
}

.newcategory{

    .list{

        .head{
            padding: 12px 0;
            text-align: center;
        }

        .sublist{
            display: flex;
            justify-content: space-around;
            align-items: center;
            flex-wrap: wrap;

            .single{
                width: 180px;
                background-color: #fff;
                margin-bottom: 5px;
                padding-bottom: 5px;

                img{
                    display: block;
                    width: 150px;
                    height: 150px;
                    margin: 0 auto;
                }

                p{
                    text-align: left;
                    padding-left: 12px;
                    @include text_over;
                }

                p:nth-child(3){
                    color: #9c3232;
                }
            }

            .last{

                .last-box{
                    width: 150px;
                    height:150px;
                    margin: 0 auto;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    flex-wrap: wrap;

                    p{
                        height: 16px;
                        width: 100%;
                        line-height: 16px;
                        color: #333;
                        font-size: 16px;
                        text-align: center;
                        padding: 0;
                        font-weight: 700;
                    }

                    .icon{
                        display: block;
                        width: 34px;
                        height: 34px;
                        background: url('./../../assets/images/icon_go_more.png') no-repeat;
                        background-size: 100% 100%;
                        margin-top: 14px;
                    }

                }
                    &::after{
                        content: '';
                        display: block;
                        width: 60px;
                        height: 60px;
                        margin: 4px 0;
                    }
            }
        }
    }
}

</style>